package org.lansir.af.view;

import java.util.Calendar;

import org.lansir.af.R;
import org.lansir.af.model.CalendarFactory;
import org.lansir.af.model.DayModel;

import android.content.Context;
import android.graphics.Canvas;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DefaultCalendarCellView extends AbstractCalendarCellView {
	private DefaultCalendarCellViewParams _paramsForAll = null;
	private DefaultCalendarCellViewParams _params = null;
	private static FrameLayout.LayoutParams WF = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.FILL_PARENT);
	private static FrameLayout.LayoutParams BG = null;
	private DayModel _model = null;
	private TextView _dayText = null;
	private View _backGround = null;
	private LinearLayout _foreGround = null;
	private CalendarCellEventListener _cellEventListener = null;
	private CalendarView _parent = null;
//	private GestureDetector _gestureDetector = null;
	private boolean _isSelected = false;

	public DefaultCalendarCellView(Context context, CalendarView parent, CalendarCellViewParam params) {
		super(context, parent);
		_parent = parent;
		_paramsForAll = (DefaultCalendarCellViewParams) params;
		_params = _paramsForAll;
		WindowManager wm = (WindowManager) (getContext().getSystemService(Context.WINDOW_SERVICE));
		Display display = wm.getDefaultDisplay();
		BG = new FrameLayout.LayoutParams(display.getWidth() / 7, display.getHeight() / 9, Gravity.CENTER);
		setWillNotDraw(false);
		addBackGroundView(context);
		addForeGroundView(context);
	}

	private void addForeGroundView(Context context) {
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		_foreGround = (LinearLayout) mInflater.inflate(R.layout.calendar_cell_foreground, null);
		//
		// _dayText =
		// (TextView)_foreGround.findViewById(R.id.tvCalendarViewContent);
//		_foreGround = new LinearLayout(context);
//		_foreGround.setGravity(Gravity.CENTER);
		_dayText = new TextView(context) {

			@Override
			protected void onDraw(Canvas canvas) {
				// TODO Auto-generated method stub
				super.onDraw(canvas);
				setBackgroundResource(getTextViewBGResource());
				// if (_model.getMark()) {
				// this.setBackgroundResource(R.drawable.calendar_marker_red);
				// }
			}
		};
		_dayText.setGravity(Gravity.CENTER);
		_foreGround.addView(_dayText);
		// _foreGround = new LinearLayout(context);
		// _foreGround.setGravity(Gravity.CENTER);
		// _foreGround.setOrientation(LinearLayout.HORIZONTAL);
		// _dayText = new TextView(context);
		// _dayText.setGravity(Gravity.CENTER);
		// _foreGround.addView(_dayText);
		// TableLayout markTable = new TableLayout(context);
		// markTable.setStretchAllColumns(true);
		// _row1 = new TableRow(getContext());
		// _row2 = new TableRow(getContext());
		// for(int i=0 ; i<_markImageModel.length ; i++){
		// _markImageModel[i] = new ImageView(getContext());
		// if(i < 4){
		// _row1.addView(_markImageModel[i], i);
		// }else{
		// _row2.addView(_markImageModel[i], i-4);
		// }
		// }
		// markTable.addView(_row1);
		// markTable.addView(_row2);
		// _foreGround.addView(markTable, WF);
		_foreGround.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				_cellEventListener.onClick(DefaultCalendarCellView.this);
			}
			
		});
//		_gestureDetector = new GestureDetector(context, new CellGestureListener(this));
//		_foreGround.setOnTouchListener(new CellOnTouchListener());
		addView(_foreGround, 1, BG);
	}

	private void addBackGroundView(Context context) {
		_backGround = new View(context) {

			@Override
			protected void onDraw(Canvas canvas) {
				super.onDraw(canvas);
				this.setBackgroundResource(getBGResource());
			}

		};
		_backGround.setWillNotDraw(false);

		
		
		addView(_backGround, 0, BG);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// Log.i("app", "onDraw:" + _model);
		super.onDraw(canvas);
	}

	@Override
	public void setDayModel(DayModel model) {
		// Log.i("app", "setDayModel:" + model);
		_model = model;
		if (_model.getCalendarCellViewParam() != null) {
			_params = (DefaultCalendarCellViewParams) _model.getCalendarCellViewParam();
		} else {
			_params = _paramsForAll;
		}

		_dayText.setText(Integer.toString(_model.getDayOfMonth()));
		setTextColor();
		setTextSize();

		// if (CalendarFactory.isShownMonth(_model.getParentMonthModel())) {
		// List<Bitmap> markList =
		// MarkImageProvider.getMarkBitmapList(_model.getMark());
		// for(int i=0 ; i<_markImageModel.length ; i++){
		// if(i <= markList.size() -1){
		// _markImageModel[i].setImageBitmap(markList.get(i));
		// }else{
		// _markImageModel[i].setImageBitmap(null);
		// }
		// }
		// }else{
		// for(int i=0 ; i<_markImageModel.length ; i++){
		// _markImageModel[i].setImageBitmap(null);
		// }
		// }
	}

	@Override
	public DayModel getDayModel() {
		return _model;
	}

	@Override
	public void setCalendarCellEventListener(CalendarCellEventListener listener) {
		_cellEventListener = listener;
	}

	private void setIsSelected(boolean isSelected) {
		if (_isSelected != isSelected) {
			invalidate();
			_cellEventListener.onSelectionChanged(this, isSelected);

		}
		_isSelected = isSelected;
	}

	private int getBGResource() {
		if (_model.isToday()) {
			return _params.getBgResourceToday();// R.drawable.calendar_today;
		} else if (Calendar.SATURDAY == _model.getDayOfWeek()) {
			return _params.getBgResourceSaturday();
		} else {
			return _params.getBgResource();// R.drawable.calendar_grid_unit;
		}
	}

	private int getTextViewBGResource() {
		if (_model.getMark()) {
			Calendar cal = Calendar.getInstance();
			if (_model.toCalendar().before(cal)) {
				return R.drawable.calendar_marker_grey;
			} else {
				return R.drawable.calendar_marker_red;
			}
		} else
			return 0;

	}

	private void setTextSize() {
		if (CalendarFactory.isShownMonth(_model.getParentMonthModel())) {
			_dayText.setTextSize(_params.getFontSizeThisMonth());
		} else {
			_dayText.setTextSize(_params.getFontSizeOtherMonth());
		}
	}

	private void setTextColor() {
		if (!CalendarFactory.isShownMonth(_model.getParentMonthModel())) {
			_dayText.setTextColor(_params.getFontColorOtherDay());
		} else if (DefaultCalendarCellViewParams.DEFAULT_FONT_COLOR_PTRIOR != _params.getFontColorPrior()) {
			_dayText.setTextColor(_params.getFontColorPrior());
		} else if (Calendar.SUNDAY == _model.getDayOfWeek()) {
			_dayText.setTextColor(_params.getFontColorSunday());
		} else if (Calendar.SATURDAY == _model.getDayOfWeek()) {
			_dayText.setTextColor(_params.getFontColorSaturday());
		} else {
			_dayText.setTextColor(_params.getFontColorOther());
		}
	}

	/*class CellGestureListener implements GestureDetector.OnGestureListener {
		DefaultCalendarCellView _view = null;

		CellGestureListener(DefaultCalendarCellView view) {
			_view = view;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			Log.i("app", "onDown");
			setIsSelected(true);
			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			Log.i("app", "onFling");
			setIsSelected(false);
			if (_parent != null) {
				if (velocityX > 2) {
					_parent.onLastMonth();
				} else if (velocityX < -2) {
					_parent.onNextMonth();
				} else {
					return false;
				}
				return true;
			} else {
				return false;
			}

		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.i("app", "onLongPress");
			if (_cellEventListener != null) {
				_cellEventListener.onLongPress(_view);
			}
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			Log.i("app", "onScroll");
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			Log.i("app", "onShowPress");
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.i("app", "onSingleTapUp");
			setIsSelected(false);
			return true;
		}
	}

	class CellOnTouchListener implements OnTouchListener {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (_gestureDetector.onTouchEvent(event)) {

			} else {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					Log.i("app", "ACTION_UP");
					setIsSelected(false);
					break;
				case MotionEvent.ACTION_DOWN:
					Log.i("app", "ACTION_DOWN");
					break;
				}
			}
			return true;
		}
	}*/
}